(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.rollover.loggedUpdateController", loggedUpdateController);
	loggedUpdateController.$inject = [ "$scope", "$q", "modalService", "layoutConfig", "validatorConfig", "authService", "loanService", "rolloverService" ];
	function loggedUpdateController($scope, $q, modalService, layoutConfig, validatorConfig, authService, loanService, rolloverService) {
		var ctrl = this, params = $scope.$stateParams.params;
		ctrl.validatorConfig = validatorConfig;
		ctrl.loan = {};

		ctrl.init = function() {
			loanService.loggedUpdateInitOptionData().then(function(initData) {
				return $q(function(resolved, rejected) {
					ctrl.prodCodeOptions = initData.prodCode;
					ctrl.loan.assetCcyOptions = initData.currency;
					ctrl.loan.prodCategoryOptions = initData.prodCategory;
					ctrl.loan.countryOptions = initData.country;
					ctrl.loan.communityIdOptions = initData.communityIdOptions;
					resolved();
				});
			}).then(function() {
				authService.loggedUpdateInit(params).then(function(response) {
					var loan = response.data.model;
					rolloverService.getLayout("rollover", "loggedUpdate", loan.loanInfo.prodType, loan.loanInfo.prodSubtype, true).then(function(layout) {
						ctrl.layoutConfig = layout || layoutConfig;
						ctrl.loan = loan || {};
						ctrl.loan.taskId = response.data.task.taskId;
						displayWfInfo(response);
					});
				});
			});
		};
		function displayWfInfo(response) {
			var messages = [];

			if (response.data.task.taskProps.wfInfo) {
				var msg = new Object();
				msg.args = response.data.task.taskProps.wfInfo.msgParams || "";
				msg.msg = response.data.task.taskProps.wfInfo.msgCode || "cls.error.unknow.exception";
				messages.push(msg);
			}
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					"messages" : messages
				});
			}
		}

		ctrl.loggedUpdateSubmitted = function() {
			return authService.loggedUpdateSubmitted(ctrl.loan).then(submitSuccessful, submitFailed);
		};

		ctrl.loggedUpdateCancelled = function() {
			modalService.openConfirmDialogs({
				"messages" : [ {
					msg : "title.dialog.confirm.cancel",
					args : [ ctrl.loan.primaryKey ]
				} ],
				"btnCallback" : {
					"ok" : function() {
						authService.loggedUpdateCancelled(ctrl.loan).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				"messages" : [ "title.dialog.successful.submit" ],
				"btnCallback" : {
					"close" : function() {
						$scope.go("cls.workflow.wfTxn.list");
					}
				},
				"redirections" : [ {
					"name" : "title.infobar.workflow.list",
					"state" : "cls.workflow.wfTxn.list",
					"params" : {}
				} ]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				"messages" : response.error
			});
		}

		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				"messages" : [ "title.dialog.successful.cancel" ],
				"btnCallback" : {
					"close" : function() {
						$scope.go("cls.workflow.wfTxn.list");
					}
				},
				"redirections" : [ {
					"name" : "title.infobar.workflow.list",
					"state" : "cls.workflow.wfTxn.list",
					"params" : {}
				} ]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				"messages" : response.error
			});
		}
	}
})(angular);
